package com.iknight.mgr.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 日期工具类
 */
/**
 * @author JeryZym
 * @date 2017/12/20
 */
public class IkgDateUtils {
    public static final String YEAR = "01";
    public static final String MONTH = "02";
    public static final String DAY = "03";

    /**
     * 获取区间日期列表
     * @param between
     * @param end
     * @param dateCycle
     * @return
     * @throws ParseException
     */
    public static List getDatePeriodList(String between, String end, String dateCycle) throws ParseException {
        if (null == dateCycle || null == between || null == end) {
            return null;
        }
        List<String> result = new ArrayList<>();
        SimpleDateFormat sdf = null;
        if (dateCycle.equals(YEAR)) {
            sdf = new SimpleDateFormat("yyyy");
        } else if (dateCycle.equals(MONTH)) {
            sdf = new SimpleDateFormat("yyyy-MM");
        } else if (dateCycle.equals(DAY)) {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
        }

        Date startDate = sdf.parse(between);
        Date endDate = sdf.parse(end);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);

        while (calendar.getTime().before(endDate)) {
            result.add(sdf.format(calendar.getTime()));
            if (dateCycle.equals(YEAR)) {
                calendar.add(Calendar.YEAR, 1);
            } else if (dateCycle.equals(MONTH)) {
                calendar.add(Calendar.MONTH, 1);
            } else if (dateCycle.equals(DAY)) {
                calendar.add(Calendar.DATE, 1);
            }
        }
        result.add(end);
        return result;
    }
}
